package com.plusjun.cart.controller;

import com.plusjun.cart.interceptor.CartInterceptor;
import com.plusjun.cart.service.CartService;
import com.plusjun.cart.to.UserInfoTO;
import com.plusjun.cart.vo.Cart;
import com.plusjun.cart.vo.CartItem;
import com.plusjun.common.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Resource
    private CartService cartService;


    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public R getCurrentUserCartItems(){
        List<CartItem> userCartItems = cartService.getUserCartItems();
        return R.ok().put("userCartItems",userCartItems);
    }

    /**
     * 浏览器中有一个 cookie：user-key(一个月过期）
     * 如果第一次使用jd的购物车功能，都会给一个临时的用户身份
     * 浏览器以后保存，每次访问都会带上这个cookie
     *
     * 登录：会有session
     * 没登陆： 按照cookie里面带来的 user-key来做
     * 第一次：如果没有临时用户，帮忙创建一个临时用户
     *
     * 去cart页面的请求
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        // 使用ThreadLocal拿到共享数据
        UserInfoTO userInfoTO = CartInterceptor.threadLocal.get();
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @param ra
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId")Long skuId,
                            @RequestParam("num")Integer num,
                            RedirectAttributes ra)  {
        try {
            cartService.addToCart(skuId,num);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ra.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到添加成功页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId")Long skuId,
                                       Model model) {
        CartItem item = cartService.getCartItem(skuId);
        model.addAttribute("item",item);
        return "success";
    }


    /**
     * 修改商品选中与未选中
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/checkItem")
    public String checkitem(@RequestParam("skuId") Long skuId,
                            @RequestParam("checked") Integer checked){

        cartService.checkitem(skuId,checked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改商品数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

}
